<!--
 * @Author: chunlai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-09-17 09:40:36
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\App.vue
 * @Description: 
-->
<template>
  <div>
    <keep-alive include="minsheng">
      <router-view v-if="pageLoad"></router-view>
    </keep-alive>
    <MyLoading :show="vLoad" :text="loadTxt" />
    <MsgDescMask v-if="pop9Desc" />
    <div v-if="disabled" class="disabledPage" @click="noAuthAction"></div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import MsgDescMask from "@/components/MsgDescMask";
import { getOrderDetail, checkStaffAsy } from "@/utils/functions.js";
import consts from "@/config/const.js";
import { filterRebackPath } from "@/methods/format.js";
import { GetRiskData } from "@/translate/risks/insert.js";
import { rrwebMixin, pagesMixin } from "@/mixins/pages";

export default {
  name: "app",
  components: { MsgDescMask },
  data() {
    return {
      staffErrMsg: "",
      disabled: false,
      pageLoad: false,
      defaultPhoneHeight: "", //屏幕默认高度
      nowPhoneHeight: "", //屏幕现在的高度
    };
  },
  computed: {
    ...mapGetters({
      vLoad: "vLoad",
      loadTxt: "loadTxt",
      vstamp: "vstamp",
      pop9Desc: "common/pop9Desc",
    }),
  },
  watch: {
    $route(to, from) {
      // console.log("to", to);
      if (to && to.path) {
        this.setWxShareConfig(to);
      }
      setTimeout(() => {
        window.scrollTo(0, 0);
      }, 10);
      this.sendDatas(this.events);
    },
    nowPhoneHeight() {
      if (this.defaultPhoneHeight != this.nowPhoneHeight) {
        //手机键盘被唤起了。
      } else {
        //  alert("手机键盘被关闭了。")
        document.activeElement.blur();
      }
    },
  },
  mixins: [rrwebMixin, pagesMixin],
  created() {
    /** 第三方地址重定向（复星智能核保） */
    const rejump = this.$getDataFromUrl("ms_redirect_uri", "2"),
      sflag = this.$getDataFromUrl("sharepage");
    if (sflag == "1" && rejump) {
      // console.log(`${decodeURIComponent(rejump)}`);
      window.location.replace(`${decodeURIComponent(rejump)}`);
      return;
    }
    // 人脸识别回跳地址参数过滤
    let h5faceId = this.$getDataFromUrl("h5faceId");
    if (h5faceId) {
      let resUrl = filterRebackPath([
        "orderNo",
        "code",
        "signature",
        "newSignature",
        "liveRate",
        "h5faceId",
      ]);
      // console.log(resUrl);
      resUrl && window.location.replace(resUrl);
      return;
    }
    this.$store.commit("updateLoad", true);
    this.$store.commit("common/CLOSE_FIELD_MEAN");
    /**重置vuex缓存 */
    const vstamp = this.$getDataFromUrl("vstamp");
    if (this.vstamp && vstamp != this.vstamp) {
      sessionStorage.clear();
      return window.location.reload();
    }
    this.$store.commit("SET_STAMP", vstamp);
    const goodsNo = this.$getDataFromUrl("goodsNo");
    if (goodsNo != this.proBldIdNo) {
      this.$store.commit("insert/SET_BLD_ID", goodsNo);
      this.$store.commit("varietyInfo/SET_RISK_CONFIG", -1);
      this.$store.commit("insert/SET_ORDER_NUM", "");
    }
    /**获取险种编码 保司名称 */
    const chanl = this.$getDataFromUrl("chanl_type");
    if (chanl) {
      this.$store.commit("insert/SET_CHANL_TYPE", chanl);
    }
    const msChal = this.$getDataFromUrl("msChannel");
    if (msChal) {
      this.$store.commit("insert/SET_MS_CHANNEL", msChal);
    }
    this.$store.commit("insert/SET_COMP_ENG", this.$getDataFromUrl("company"));
    this.$store.commit(
      "insert/SET_MAIN_RISK_CODE",
      this.$getDataFromUrl("mcode")
    );
    this.$store.commit("insert/SET_WORK_NUM", this.$getDataFromUrl("workNum"));
    this.$store.commit("insert/SET_BLD_UID", this.$getDataFromUrl("userId"));
    this.$store.dispatch("json/getPageConfig").then((res) => {
      this.initPage(
        this.$getDataFromUrl("workNum"),
        this.$getDataFromUrl("mcode")
      );
    });
    let that = this;
    window.addEventListener("popstate", function (event) {
      console.log("popstate: " + JSON.stringify(event));
      that.sendDatas(that.events);
    });
    // console.log(this.$GlobalFuns.userAge("2006-09-04"))
  },
  mounted() {
    this.scrollIntoView();
  },
  methods: {
    async initPage(workNum, mainCode) {
      /**页面地址 */
      this.$store.dispatch("json/getDictConfig");
      /**检查代理人资质（获取代理人信息） */
      if (workNum && mainCode && !this.orderNum) {
        // 已经获取订单号，不重复调用校验接口
        let res = await this.staffAuthCheck(workNum, mainCode);
        if (res) return;
      }
      /**获取险种信息 */
      if (mainCode) {
        this.$store.commit("insert/SET_MAIN_RISK_CODE", mainCode);
        let apiPams = {
          mainRiskCode: mainCode,
          workNum: workNum,
        };
        if (this.proBldIdNo) {
          apiPams.goodsNo = this.proBldIdNo;
        }
        let data = await this.$store.dispatch("apis/comAdminApiRequest", {
          apiParams: apiPams,
          apiName: "getVarietyInfo",
          toast: true,
        });
        if (data.code != 1) return;
        /**获取订单号-继续投保 */
        let orderNum = this.$getDataFromUrl("orderNum");
        if (orderNum) {
          this.$store.commit("insert/SET_ORDER_NUM", orderNum);
          const res2 = await getOrderDetail(this.apiComParams),
            res2Data = res2.data;
          if (res2Data.code == "1") {
            this.$store.commit("page/SET_INDEX_GZ_FLAG", "1");
            this.backDetailToStore(res2Data);
          } else {
            // 智能告知分享结果页不显示
            const rname = this.$route.path;
            if (!["/shareres"].includes(rname)) {
              this.$msAlert(res2Data.msg);
            }
          }
        } else {
          let res3 = await this.getOrderNum(mainCode, workNum);
        }
      }
      this.$store.commit("updateLoad", false);
      this.$isResolve();
      let hashurl = window.location.hash;
      // console.log(hashurl);
      if (hashurl.indexOf("#/index") == 0 || hashurl == "#/") {
        const ptype = this.$getDataFromUrl("ptype");
        if (ptype) {
          this.$router.replace({ path: `/${ptype}` });
        }
      }
      this.$nextTick(() => {
        this.openRrweb();
      });
      await this.$onLaunched;
      this.$nextTick(() => {
        this.pageLoad = true;
      });
    },
    //获取订单号
    getOrderNum(mrCode, workNum) {
      return new Promise((reslove, reject) => {
        if (this.orderNum) return reslove("1");
        let params = {
          mainRiskCode: mrCode,
          workNum: workNum,
        };
        this.$http
          .post("/ms-insure/api/common/aquireOrderNum", params, {
            json: true,
          })
          .then(({ data }) => {
            if (data.code == 1) {
              this.$store.commit("insert/SET_ORDER_NUM", data.orderNum);
            } else {
              this.$dialog.alert({
                message: data.msg,
              });
            }
            reslove("1");
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    /**检查代理人资质获取业务员信息 */
    async staffAuthCheck(workNum, mainCode) {
      let params = {
        mainRiskCode: mainCode,
        workNum: workNum,
      };
      if (this.proBldIdNo) {
        params.goodsNo = this.proBldIdNo;
      }
      this.disabled = false;
      const staffRes = await checkStaffAsy(params);
      const resData = staffRes.data;
      // console.log("staffRes----", staffRes);
      let staffErrMsg = "";
      if (resData.code === "1") {
        this.$store.commit("agent/SET_STATE_FIELD", resData.agent);
      } else {
        if (resData.availableForSale && resData.availableForSale == "1") {
          staffErrMsg = "您暂无销售权限,请确认权限后重试!";
        }
        if (resData.code && resData.code == "0") {
          staffErrMsg = resData.msg;
        }
      }
      if (staffErrMsg) {
        this.$store.commit("page/SET_INDEX_GZ_FLAG", "1");
        this.staffErrMsg = staffErrMsg;
        this.disabled = true;
        this.$dialog.alert({
          message: staffErrMsg,
        });
        this.$store.commit("updateLoad", false);
      }
      return staffErrMsg;
    },
    /**无权限早操作----废弃 */
    noAuthAction() {
      if (this.disabled) {
        this.$msAlert(
          `${
            this.staffErrMsg ||
            "对不起，您当前无权限销售此产品，请联系人管开放销售权限"
          }`
        );
      }
    },
    backDetailToStore(payload) {
      let { appnt, cont, insureds } = payload;
      // 核保禁止修改--->判断页面跳转/能否修改信息
      if (payload.flag !== void 0) {
        if (["-1", "1", "3", "4", "5", "6"].includes(payload.flag)) {
          this.$store.commit("insert/SET_FORBID_ALTER", true);
        }
        if (this.compEngName == "ansheng") {
          this.$store.commit("json/SET_AS_BILL_STATUS", payload.flag);
        }
      }
      if (appnt) {
        this.$store.commit("insert/UPDATE_APPLICANT", consts.delNull(appnt));
      }
      if (cont) {
        // 保单状态--->确认页/支付页,头部信息显示
        if (cont.orderStatus !== void 0) {
          this.$store.commit("insert/SET_INSURE_FLAG", {
            code: cont.orderStatus || "",
            msg: cont.orderRemark || "",
          });
        }
        // 双录
        if (cont.needDouble !== void 0) {
          this.$store.commit("insert/SET_DOUBLE_VIDEO", cont.needDouble || "0");
        }
        // 万能险回显缓存
        if (cont.relatedPrtNo) {
          let obj = {
            serialNumber: cont.relatedPrtNo,
            applicantName: appnt.name,
          };
          this.$setMessage("relaDatas", obj);
        }
        this.$store.commit("insert/UPDATE_PAYINFO", consts.delNull(cont));
        //续保
        if (cont.renewFlag !== void 0 && cont.renewFlag !== "") {
          this.$store.dispatch("insert/apiRebackXuBao", consts.delNull(cont));
        }
      }
      if (insureds) {
        this.$store.dispatch("insert/apiRebackBbrSyr", insureds);
        if (insureds[0]) {
          let risks = insureds[0].risks;
          if (risks) {
            if (this.compEngName === "ansheng") {
              // 安盛产品继续投保回显(2021-12-10)
              this.$store.dispatch("risks/backRiskSetAnSheng", risks);
            } else {
              this.$store.commit("insert/SET_RECALL", "0");
              let risk = GetRiskData(risks);
              this.$store.commit("risks/SET_RISKS_DATA", risk);
            }
          }
        }
      }
    },
    /**回溯视屏弹窗键盘防止页面高度崩塌*/
    scrollIntoView() {
      this.defaultPhoneHeight = window.innerHeight;
      window.addEventListener("resize", () => {
        window.setTimeout(function () {
          let ua = navigator.userAgent;
          let iOS = /iPad|iPhone|iPod/.test(ua);
          this.nowPhoneHeight = window.innerHeight;
          if (this.nowPhoneHeight < this.defaultPhoneHeight - 50) {
            if (iOS) {
              document.body.scrollTop = document.body.scrollHeight + 50;
            } else {
              input.scrollIntoView(true);
            }
          }
        }, 50);
      });
    },
  },
};
</script>
<style lang="less" scoped>
.disabledPage {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 990;
}
</style>
